package com.mandao.oneplat.boot.context;

import org.apache.commons.lang3.math.NumberUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunson on 2017/3/21.
 */
public class OneRequest {
    // 系统信息
    private String uri;
    private boolean mobile;
    private String ip;
    private String lang = "zh";
    // 参数信息
    private Map<String, String> params;
    // Cookie信息
    private Map<String, String> cookies;
    // 经过path解析后的实际业务路径
    private String pathUri;

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public boolean isMobile() {
        return mobile;
    }

    public void setMobile(boolean mobile) {
        this.mobile = mobile;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    public Map<String, String> getParams() {
        return params;
    }

    public Map<String, String> getCookies() {
        return cookies;
    }

    public String getPathUri() {
        return pathUri;
    }

    public void setPathUri(String pathUri) {
        this.pathUri = pathUri;
    }

    public void addParams(Map<String, String> params) {
        if (params == null || params.isEmpty()) {
            return;
        }
        if (this.params == null) {
            this.params = new HashMap<>(params);
        } else {
            this.params.putAll(params);
        }
    }

    public String getParam(String name) {
        if (params == null || !params.containsKey(name)) {
            return null;
        }
        return params.get(name);
    }

    public int getParamAsInt(String name) {
        return NumberUtils.toInt(getParam(name));
    }

    public long getParamAsLong(String name) {
        return NumberUtils.toLong(getParam(name));
    }

    public void addCookie(String name, String value) {
        if (cookies == null) {
            cookies = new HashMap<>();
        }
        cookies.put(name, value);
    }

    public String getCookie(String name) {
        if (cookies == null || !cookies.containsKey(name)) {
            return null;
        }
        return cookies.get(name);
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("uri=").append(getUri());
        sb.append(",mobile=").append(isMobile());
        sb.append(",ip=").append(getIp());
        sb.append(",lang=").append(getLang());
        sb.append(",pathUri=").append(getPathUri());
        sb.append(",params=").append(getParams());
        sb.append(",cookies=").append(getCookies());
        return sb.toString();
    }
}
